package com.fpmsa.officeweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fpmsa.officeweb.constant.OWConstant;
import com.fpmsa.officeweb.core.util.DateUtil;
import com.fpmsa.officeweb.core.util.ToolUtil;
import com.fpmsa.officeweb.dao.NewsMapper;
import com.fpmsa.officeweb.model.News;
import com.fpmsa.officeweb.model.NewsCategory;
import com.fpmsa.officeweb.service.INewsCategoryService;
import com.fpmsa.officeweb.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 官网文章表 服务实现类
 * </p>
 *
 * @author huangyuehong123
 * @since 2018-06-14
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService {

    @Autowired
    private INewsCategoryService newsCategoryService;


    /**
     * 根据条件获取文章列表
     */
    @Override
    public List<Map<String, Object>> getNewsList(Page<News> page, String beginTime, String endTime, String title, String orderByField, boolean asc) {
        return this.baseMapper.getNewsList(page,beginTime,endTime,title,orderByField,asc);
    }

    /**
     * 获取更多的新闻(排除当前新闻)
     */
    @Override
    public List<Map<String, Object>> getMoreNews(int num, boolean isDesc, Long id) {
        return this.baseMapper.getMoreNews(num,isDesc,id);
    }

    /**
     * 查找“置顶”状态的新闻的主键id
     */
    @Override
    public Long getTheTopNewsId(Long category) {

        EntityWrapper<News> wrapper = new EntityWrapper<>();
        wrapper.eq("category",category);
        wrapper.eq("is_del",0);
        wrapper.eq("is_top",1);

        List<News> newsList = selectList(wrapper);
        if (ToolUtil.isNotEmpty(newsList)){
            Long topNewsId = newsList.get(0).getId();
            return topNewsId;
        }else {
            return null;
        }
    }

    /**
     * 根据主键id将某一条新闻“置顶”
     */
    @Override
    public void setNewsIsNotTop(Long id) {
        this.baseMapper.setNewsIsNotTop(id);
    }

    /**
     * 将news对象list中的所有create_time转换成“年”和“月日”两部分，其他的值不变
     */
    @Override
    public List<Map<String, Object>> formatCreateTime(List<News> newsList) {
        List<Map<String,Object>> newsMapList = new ArrayList<>();
        for (News news : newsList){
            Map<String,Object> map = new HashMap<>();
            map.put("id",news.getId());
            map.put("year",DateUtil.getYMDByDate(news.getCreateTime()).get("year"));
            map.put("monthDay",DateUtil.getYMDByDate(news.getCreateTime()).get("month")+"/"+DateUtil.getYMDByDate(news.getCreateTime()).get("day"));
            map.put("title",news.getTitle());
            map.put("category",news.getCategory());
            map.put("description",news.getDescription());
            map.put("transmitFrom",news.getTransmitFrom());
            map.put("author",news.getAuthor());
            map.put("content",news.getContent());
            map.put("picture",news.getPicture());
            map.put("keyword",news.getKeyword());
            map.put("readtimes",news.getReadtimes());
            map.put("isShow",news.getIsShow());
            map.put("isTop",news.getIsTop());

            // 获取新闻创建时间和当前时间的差值
            Long l = System.currentTimeMillis() - news.getCreateTime().getTime();
            long year = l/(31536000000l);
            long month = l/(2628000000l);
            long day = l/(24*60*60*1000);
            long hour = l/(60*60*1000);
            long min = l/(60*1000);

            if (year >= 1){
                map.put("interval",year+"年前");
            }else if(month >= 1){
                map.put("interval",month+"个月前");
            }else if (day >= 1){
                map.put("interval",day+"天前");
            }else if(hour >= 1){
                map.put("interval",hour+"小时前");
            }else if(min >= 1){
                map.put("interval",min+"分钟前");
            }else{
                map.put("interval","刚刚");
            }

            newsMapList.add(map);
        }
        return newsMapList;
    }

    /**
     * 获取当前新闻的关联新闻(排除当前新闻)
     */
    @Override
    public List<News> getRelatedNews(int num, boolean isAsc, Long id,Long category) {

        EntityWrapper<News> wrapper = new EntityWrapper<>();
        wrapper.eq("category",category);
        wrapper.notIn("id",id);
        wrapper.eq("is_del",0);
        wrapper.orderBy("create_time",isAsc);
        wrapper.last("limit "+num);
        List<News> relatedNews = selectList(wrapper);

        return relatedNews;
    }

    /**
     * 获取当前新闻的下一篇（比较新闻创建时间，较早的非删除状态的第一篇为下一篇）
     */
    @Override
    public News getNextNews(Date create_time,Long category) {
        EntityWrapper<News> wrapper = new EntityWrapper<>();
        wrapper.lt("create_time",create_time);
        wrapper.eq("category",category);
        wrapper.eq("is_del",0);
        wrapper.orderBy("create_time",false);
        wrapper.last("limit 1");

        News nextNews = selectOne(wrapper);
        return nextNews;
    }

    /**
     * 获取某一类别的新闻的条数
     */
    @Override
    public Integer getCountOfCategory(String categoryName) {
        EntityWrapper<NewsCategory> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("name",categoryName);
        wrapper1.eq("is_del",0);
        Long category = null;
        if (newsCategoryService.selectOne(wrapper1) != null){
            category = newsCategoryService.selectOne(wrapper1).getId();
        }

        Integer countNews = this.baseMapper.getCountOfCategory(category);
        return countNews;
    }


    /**
     * 获取某一时间之前的num条新闻
     */
    @Override
    public List<News> getNewsBeforeTimes(Long categoryId,String condition,Date create_time,Integer num,Boolean isAsc) {

        EntityWrapper<News> wrapper = new EntityWrapper<>();

        // 当新闻类别为“search”时表示搜索功能的“浏览更多”，这时不需要区分类别
        if (categoryId != null){
            wrapper.eq("category",categoryId);
        }else {
            wrapper.notIn("category",11);// 去掉“资质荣誉”类别
        }

        wrapper.lt("create_time",create_time);

        // “搜索”时的“浏览更多”需要加上输入的搜索词--condition作为筛选条件
        if (condition != null){
            wrapper.like("title",condition);
        }

        wrapper.eq("is_del",0);
        wrapper.orderBy("create_time",isAsc);
        if (num != null){
            wrapper.last("limit "+num);
        }

        List<News> newsList = selectList(wrapper);
        return newsList;
    }

    /**
     * 获取对应年份条件下有数据的“年月”列表
     */
    @Override
    public List<Date> getYearAndMonth(Integer year) {
        return this.baseMapper.getYearAndMonth(year);
    }

    /**
     * 获取对应年份下的所有“大事记”记录
     */
    @Override
    public List<Map<String, Object>> getHistories(Integer year) {
        return this.baseMapper.getHistories(year);
    }

    /**
     *  获取所有的会员风采相关的新闻
     */
    @Override
    public List<News> getAllMMNews(Integer num) {
        Long category = newsCategoryService.getIdByName(OWConstant.OW_MEMBER_DEMEANOUR);
        EntityWrapper<News> wrapper = new EntityWrapper<>();
        wrapper.eq("is_del",0);
        wrapper.eq("category",category);
        if (num != null){
            wrapper.last("limit " + num);
        }
        List<News> newsList = selectList(wrapper);

        return newsList;
    }


}
